//
//  MainInfoSearchViewController.m
//  Jyxt
//
//  Created by 刘日 on 17/5/10.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "MainInfoSearchViewController.h"
#import "DatabaseTool.h"
#import <WebKit/WebKit.h>
#import "LemonMixView.h"

@interface MainInfoSearchViewController () <UIScrollViewDelegate , UITextFieldDelegate>

@property (nonatomic , strong) NSArray<NSNumber *> *locations;
@property (nonatomic , strong) NSArray<UILabel *> *labels;
@end

@implementation MainInfoSearchViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.lkNavigationBar.barTintColor = [ExtenColor colors].c_f5f5f5;
    [self.lkNavigationBar setTitleColor: [ExtenColor colors].c_cccccc];
    self.lkNavigationBar.clipsToBounds = YES;
}

- (void)initView{
    self.searchArea.layer.shadowColor = [UIColor blackColor].CGColor;
    self.searchArea.layer.shadowOpacity = 0.05;
    self.searchArea.layer.shadowOffset = CGSizeMake(0,4);
    self.searchArea.layer.shadowRadius = 16;
    
    [self.view addSubview: self.backScrollView];
    [self.view sendSubviewToBack: self.backScrollView];
    [self.view addSubview: self.indexIconView];
    
    self.searchField.delegate = self;
    self.backScrollView.showsHorizontalScrollIndicator = NO;
    
    for (NSInteger i = 0; i < self.labels.count; i ++) {
        self.labels[i].onTap = ^(UIView *view){
            [self.backScrollView setContentOffset: CGPointMake(i * self.backScrollView.frame.size.width, 0) animated: YES];
        };
    }
    
    for(NSInteger i = 0 ; i < 3 ; i ++){
        LemonMixView *wbv = [[LemonMixView alloc] initWithFrame: CGRectMake(i * self.backScrollView.frame.size.width, 226, self.backScrollView.frame.size.width, 200)];
        wbv.tag = 3000 + i;
        NSString *key = [NSString stringWithFormat: @"RECORD%ld" , i];
        NSString *record = [DatabaseTool getWithKey: key];
        record = (record ? record : @"");
        wbv.backgroundColor = [UIColor clearColor];
        ((WKWebView *) [wbv valueForKey: @"lmWebView"]).scrollView.bounces = NO;
        [wbv.on addOnLoadCompleteCallback:^(LemonMixView *mixView) {
            [mixView.operate evaluateJavaScript: [NSString stringWithFormat: @"setHistory('%@')" , record] complete:^(id  _Nullable result, NSError * _Nullable error) {
            }];
        }];
        __block MainInfoSearchViewController *_s = self;
        [wbv.on addOnReceiveJSMsgCallback:^(LemonMixView *mixView, NSDictionary *msg) {
            NSLog(@"msg = = = = = = = ***  %@" , msg);
            self.searchField.text = msg[@"data"][@"value"];
            [_s textFieldShouldReturn: self.searchField];
        }];
        [wbv.operate loadUrl: HTMLURL(HTML_SEARCH_SEARCH_HISTORY)];
        [self.backScrollView addSubview: wbv];
    }
}
- (IBAction)onClearHistory:(id)sender {
    NSInteger current = floor(self.backScrollView.contentOffset.x / self.backScrollView.frame.size.width);
    NSString *key = [NSString stringWithFormat: @"RECORD%ld" , current];
    [DatabaseTool putWithKey: key value: @""];
    [((LemonMixView *)[self.backScrollView viewWithTag: 3000 + current]).operate evaluateJavaScript: [NSString stringWithFormat: @"setHistory('%@')" , @""] complete:^(id  _Nullable result, NSError * _Nullable error) {
    }];
}

- (UIImageView *)indexIconView{
    if (!_indexIconView) {
        _indexIconView = [[UIImageView alloc] initWithFrame:CGRectMake(175, 104, 12, 6)];
        _indexIconView.image = [UIImage imageNamed: @"info_search_index"];
    }
    return _indexIconView;
}

- (UIScrollView *)backScrollView{
    if (!_backScrollView) {
        _backScrollView = [[UIScrollView alloc] initWithFrame: self.view.frame];
        _backScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
        _backScrollView.pagingEnabled = YES;
        _backScrollView.bounces = NO;
        _backScrollView.delegate = self;
        NSArray *nameArr = @[@"info_search_contract_back" , @"info_search_person_back" , @"info_search_house_back"];
        for (NSInteger i = 0; i < nameArr.count; i ++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake((i + 1) * self.view.frame.size.width - 200, self.view.frame.size.height - 300, 160, 160)];
            imageView.alpha = 0.1;
            imageView.image = [UIImage imageNamed: nameArr[i]];
            [_backScrollView addSubview: imageView];
        }
        
    }
    return _backScrollView;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (NSArray<NSNumber *> *)locations{
    if (!_locations) {
        _locations = @[@44 , @100 , @175  , @175];
    }
    return _locations;
}

- (NSArray<UILabel *> *)labels{
    if (!_labels) {
        _labels = @[self.contractLabel , self.personLabel , self.houseLabel];
    }
    return _labels;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger current = floor(scrollView.contentOffset.x / scrollView.frame.size.width);
    self.searchField.placeholder = @[@"请输入合同编号" , @"请输入实有人口姓名" , @"请输入房源地址"][current];
    self.clearButton.tag = current;
    double progress = scrollView.contentOffset.x / scrollView.frame.size.width - current;
    self.indexIconView.frame = CGRectMake([self.locations[current] floatValue] + ([self.locations[current + 1] floatValue] - [self.locations[current] floatValue]) * progress, self.indexIconView.frame.origin.y, self.indexIconView.frame.size.width, self.indexIconView.frame.size.height);
    for (NSInteger i = 0; i < 3;  i ++) {
        self.labels[i].font = (i == current && progress == 0) ? [UIFont boldSystemFontOfSize: 14] : [UIFont systemFontOfSize: 14];
        self.labels[i].textColor = (i == current && progress == 0) ? [UIColor themeColor] : [UIColor blackColor];
    }
    NSLog(@"CURRENT:%ld - %f" , current , progress);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    // 搜索
    NSLog(@"搜索了！%@" , textField.text);
    NSInteger current = floor(self.backScrollView.contentOffset.x / self.backScrollView.frame.size.width);
    MIX_OPEN(([NSString stringWithFormat: @"%@?keywords=%@" , @[HTML_SEARCH_CONTRACT_LIST,HTML_SEARCH_PERSON_LIST,HTML_SEARCH_HOUSE_LIST][current] , textField.text]));
    NSString *key = [NSString stringWithFormat: @"RECORD%ld" , current];
    NSString *record = [DatabaseTool getWithKey: key];
    record = (record ? record : @"");
    NSMutableArray<NSString *> *items = [[NSMutableArray alloc] initWithArray:[record componentsSeparatedByString: @","]];
    [items insertObject: textField.text atIndex: 0];
    if (items.count == 11)
        [items removeLastObject];
    record = [items componentsJoinedByString: @","],
    [DatabaseTool putWithKey: key value: record];
    [((LemonMixView *)[self.backScrollView viewWithTag: 3000 + current]).operate evaluateJavaScript: [NSString stringWithFormat: @"setHistory('%@')" , record] complete:^(id  _Nullable result, NSError * _Nullable error) {
    }];
    textField.text = @"";
    [textField resignFirstResponder];
    return YES;
}

@end
